//
//  LoginViewController.swift
//  SwiftForwardWayZhe
//
//  Created by 刘喆 on 2022/1/7.
//

import UIKit
import SwiftUI

class LoginViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource{

    
    
    let width = (ScreenWidth - 125)/2
    
    let regionArr = ["广西","全国","十五省","河北廊坊"]
    let roleArr = ["筛查员","质控员","校医","班主任"]
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.setupUI()
    }
    
    func setupUI() {
        
        self.loginImg.image = UIImage.init(named: "loginimg")
        self.regionTf.placeholder = "请选择地区"
        self.regionTf = UITextField.setPlaceholder(textField: self.regionTf, color: kAPPlightGrayColor, font: 15)
        self.regionBtn.backgroundColor = UIColor.clear
        self.regionarrow.image = UIImage.init(named: "arrow_down")
        self.regionLine.backgroundColor = kRGBColorFromHex(rgbValue: 0xE8ECEB)
        
        self.roleTf.placeholder = "请选择角色"
        self.roleTf = UITextField.setPlaceholder(textField: self.roleTf, color: kAPPlightGrayColor, font: 15)
        self.roleBtn.backgroundColor = UIColor.clear
        self.rolearrow.image = UIImage.init(named: "arrow_down")
        self.roleLine.backgroundColor = kRGBColorFromHex(rgbValue: 0xE8ECEB)
        
        self.accountTf.placeholder = "请输入账号";
        self.accountTf = UITextField.setPlaceholder(textField: self.accountTf, color: kAPPlightGrayColor, font: 15)
        self.accountLine.backgroundColor = kRGBColorFromHex(rgbValue: 0xE8ECEB)
        
       
        self.pwdTf.placeholder = "请输入密码";
        self.pwdTf = UITextField.setPlaceholder(textField: self.pwdTf, color: kAPPlightGrayColor, font: 15)
        self.pwdLine.backgroundColor = kRGBColorFromHex(rgbValue: 0xE8ECEB)
        self.eyeBtn.backgroundColor = UIColor.white
        self.loginBtn.backgroundColor = kAppStyleColor
        self.howBtn.backgroundColor = UIColor.white
        
        self.view.addSubview(self.versionL)
        versionL.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-50);
        }
        
        self.regionTable.backgroundColor = UIColor.white
        self.roleTable.backgroundColor = UIColor.white
        
        
        
    }
    

    
     lazy var loginImg: UIImageView = {
        let loginImg = UIImageView.init()
         self.view.addSubview(loginImg)
         loginImg.snp.makeConstraints{ make in
             make.top.equalTo(self.view).offset(125)
             make.width.equalTo(222)
             make.height.equalTo(55)
             make.centerX.equalTo(self.view)
         }
        return loginImg
    }()
    
  
    //MARK----地区lazy
    lazy var regionTf: UITextField = {
        let regionTf = UITextField.init()
        self.view.addSubview(regionTf)
        regionTf.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(50)
            make.width.equalTo(width)
            make.top.equalTo(loginImg.snp.bottom).offset(70)
            make.height.equalTo(30)
        }
        regionTf.textColor = kAPPlightGrayColor
        regionTf.font = UIFont.systemFont(ofSize: 25)

        return regionTf
    }()
    
    lazy var regionarrow: UIImageView = {
       let regionarrow = UIImageView.init()
       self.view.addSubview(regionarrow)
        regionarrow.snp.makeConstraints{ make in
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.right.equalTo(self.regionTf)
            make.centerY.equalTo(self.regionTf)
       }
       return regionarrow
   }()
    
    lazy var regionBtn: UIButton = {
        let regionBtn = UIButton.init(type: .custom)
        self.view.addSubview(regionBtn)
        regionBtn.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(self.regionTf)
        }
        regionBtn.addTarget(target, action: #selector(showregionList), for: .touchUpInside)
        return regionBtn

    }()
    
    lazy var regionLine: UIView = {
        let regionLine = UIView.init()
        self.view.addSubview(regionLine)
        regionLine.snp.makeConstraints { make in
            make.left.right.equalTo(self.regionTf)
            make.top.equalTo(self.regionTf.snp.bottom).offset(10)
            make.height.equalTo(1)
        }
        return regionLine
        
    }()
    
    
    
    //MARK----角色lazy
    lazy var roleTf: UITextField = {
        let roleTf = UITextField.init()
        self.view.addSubview(roleTf)
        roleTf.snp.makeConstraints { make in
            make.left.equalTo(self.regionTf.snp.right).offset(25)
            make.width.equalTo(width)
            make.top.equalTo(self.loginImg.snp.bottom).offset(70)
            make.height.equalTo(30)
        }
        roleTf.textColor = kAPPlightGrayColor
        roleTf.font = UIFont.systemFont(ofSize: 25)
        
        return roleTf
    }()
    
    lazy var rolearrow: UIImageView = {
       let rolearrow = UIImageView.init()
       self.view.addSubview(rolearrow)
        rolearrow.snp.makeConstraints{ make in
            make.width.equalTo(20)
            make.height.equalTo(20)
            make.right.equalTo(self.roleTf)
            make.centerY.equalTo(self.roleTf)
       }
       return rolearrow
   }()
    
    lazy var roleBtn: UIButton = {
        let roleBtn = UIButton.init(type: .custom)
        self.view.addSubview(roleBtn)
        roleBtn.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(self.roleTf)
        }
        roleBtn.addTarget(target, action: #selector(showregionList), for: .touchUpInside)
        return roleBtn

    }()
    
    lazy var roleLine: UIView = {
        let roleLine = UIView.init()
        self.view.addSubview(roleLine)
        roleLine.snp.makeConstraints { make in
            make.left.equalTo(self.roleTf)
            make.right.equalTo(self.roleTf)
            make.top.equalTo(self.regionTf.snp.bottom).offset(10)
            make.height.equalTo(1)
        }
        return roleLine
    }()


    //MARK-----
    lazy var accountTf: UITextField = {
        let accountTf = UITextField.init()
        self.view.addSubview(accountTf)
        accountTf.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(56.5)
            make.right.equalTo(self.view).offset(-45)
            make.top.equalTo(self.regionLine.snp.bottom).offset(20)
            make.height.equalTo(30)
        }
        accountTf.textColor = kAPPdarkGrayColor
        accountTf.font = UIFont.systemFont(ofSize: 25)
        
        return accountTf
    }()
    
    lazy var accountLine: UIView = {
        let accountLine = UIView.init()
        self.view.addSubview(accountLine)
        accountLine.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(42.5);
            make.right.equalTo(self.view).offset(-42.5);
            make.top.equalTo(self.accountTf.snp.bottom).offset(10);
            make.height.equalTo(1);
        }
        return accountLine
    }()
    
    
    lazy var pwdTf: UITextField = {
        let pwdTf = UITextField.init()
        self.view.addSubview(pwdTf)
        pwdTf.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(56.5)
            make.right.equalTo(self.view).offset(-45)
            make.top.equalTo(self.accountLine.snp.bottom).offset(20)
            make.height.equalTo(44);
        }
        pwdTf.textColor = kAPPdarkGrayColor
        pwdTf.font = UIFont.systemFont(ofSize: 25)
        
        return pwdTf
    }()
    
    
    lazy var eyeBtn: UIButton = {
        let eyeBtn = UIButton.init(type: .custom)
        self.view.addSubview(eyeBtn)
        eyeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(22)
            make.right.equalTo(self.view).offset(-45)
            make.centerY.equalTo(self.pwdTf)
        }
        eyeBtn.setBackgroundImage(UIImage.init(named: "eyeClosed"), for: .normal)
        eyeBtn.setBackgroundImage(UIImage.init(named: "eyeOpen"), for: .selected)
        eyeBtn.addTarget(target, action: #selector(eyeBtnClicked), for: .touchUpInside)
        return eyeBtn

    }()
    
    lazy var pwdLine: UIView = {
        let pwdLine = UIView.init()
        self.view.addSubview(pwdLine)
        pwdLine.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(42.5)
            make.right.equalTo(self.view).offset(-42.5)
            make.top.equalTo(self.pwdTf.snp.bottom).offset(10)
            make.height.equalTo(1)
        }
        return pwdLine
    }()
    
    lazy var loginBtn: UIButton = {
        let loginBtn = UIButton.init(type: .custom)
        self.view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { make in
            make.top.equalTo(self.pwdLine.snp.bottom).offset(30)
            make.width.equalTo(290)
            make.height.equalTo(40)
            make.centerX.equalTo(self.view)
        }
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.titleLabel?.font = UIFont.init(name: kFontPingfangRegular, size: 17)
        loginBtn.addTarget(target, action: #selector(login), for: .touchUpInside)
        loginBtn.layer.cornerRadius = 20;
        loginBtn.layer.masksToBounds = true;
        
        return loginBtn

    }()
    
    lazy var howBtn: UIButton = {
        let howBtn = UIButton.init(type: .custom)
        self.view.addSubview(howBtn)
        howBtn.snp.makeConstraints { make in
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.right.equalTo(self.loginBtn)
            make.top.equalTo(self.loginBtn.snp.bottom).offset(15)
        }
 
        let attrStr = NSMutableAttributedString.init(string: "如何获得登录账号?")
        let range = NSRange(location: 0, length: attrStr.length)
        attrStr.addAttributes([NSAttributedString.Key.font : UIFont.init(name: kFontPingfangRegular, size: 11)!,NSAttributedString.Key.foregroundColor: kAppStyleColor], range: range)
        attrStr.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: range)
        howBtn.setAttributedTitle(attrStr, for: .normal)
        howBtn.addTarget(target, action: #selector(GetAnAccount), for: .touchUpInside)
        
        return howBtn

    }()

    lazy var versionL: UILabel = {
        
        let versionL = UILabel.init()
        versionL.text = "当前版本 : v\(APPVersion)"
        versionL.textAlignment = .center
        versionL.font = UIFont.init(name: kFontPingfangRegular, size: 14)
        
        return versionL
    }()
    
    
    lazy var regionTable: UITableView = {
        
        let regionTable = UITableView.init()
        self.view.addSubview(regionTable)
        regionTable.snp.makeConstraints { make in
            make.left.equalTo(self.regionTf)
            make.top.equalTo(self.regionTf.snp.bottom)
            make.width.equalTo(width)
            make.height.equalTo(4*44)
        }
        regionTable.delegate = self
        regionTable.dataSource = self
        regionTable.layer.cornerRadius = 5
        regionTable.layer.borderWidth = 0.5
        regionTable.layer.borderColor = kRGBColorFromHex(rgbValue: 0x666666).cgColor
        regionTable.layer.masksToBounds = true
        regionTable.isHidden = true
        return regionTable
        
    }()
    
    lazy var roleTable: UITableView = {
        
        let roleTable = UITableView.init()
        self.view.addSubview(roleTable)
        roleTable.snp.makeConstraints { make in
            make.left.equalTo(self.roleTf)
            make.top.equalTo(self.roleTf.snp.bottom)
            make.width.equalTo(width)
            make.height.equalTo(4*44)
        }
        roleTable.delegate = self
        roleTable.dataSource = self
        roleTable.layer.cornerRadius = 5
        roleTable.layer.borderWidth = 0.5
        roleTable.layer.borderColor = kRGBColorFromHex(rgbValue: 0x666666).cgColor
        roleTable.layer.masksToBounds = true
        roleTable.isHidden = true
        return roleTable
        
    }()
    
    
    
    //MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell.init(style: .default, reuseIdentifier: "UITableViewCell")
        
        if tableView == self.regionTable {
            cell.textLabel?.text = self.regionArr[indexPath.row]
        } else {
            cell.textLabel?.text = self.roleArr[indexPath.row]
        }
        
        cell.backgroundColor = UIColor.white
        cell.selectionStyle = .none
 

        return cell;
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if tableView == self.regionTable {
            UserDefaults.standard.setValue(String(indexPath.row), forKey: "useRegion")
            UserDefaults.standard.synchronize()
            self.regionTf.text = self.regionArr[indexPath.row]
            self.regionTable.isHidden = true
        } else {
            UserDefaults.standard.setValue(String(indexPath.row), forKey: "roleNumber")
            UserDefaults.standard.synchronize()
            self.roleTf.text = self.roleArr[indexPath.row]
            self.roleTable.isHidden = true
        }
        
        
    }

    ///MARK: ---- Action
    @objc override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        if self.regionTable.isHidden == false {
            UIView.animate(withDuration: 0.3) {
                self.regionTable.alpha = 0
            } completion: { _ in
                self.regionTable.isHidden = true
            }
        }
        if self.roleTable.isHidden == false {
            UIView.animate(withDuration: 0.3) {
                self.roleTable.alpha = 0
            } completion: { _ in
                self.roleTable.isHidden = true
            }
        }
   
        self.view.endEditing(true)
    }
    
    //MARK: - 隐藏所有选择视图
    private func hiddenForChooseView() -> Void {
        
        self.regionTable.isHidden = true
        self.roleTable.isHidden = true
        self.view.endEditing(true)
    }
    
    
    
    @objc func login(){
        
        
        let params: NSDictionary = ["phone":self.accountTf.text!,"pw":self.pwdTf.text!]
        NetworkProvider.request(NetworkAPI.login_national(params: params)) { result in
            
            if case .success(let response) = result {
                           
                let jsonDic = try! response.mapJSON() as! NSDictionary
                print(jsonDic)
                
            }
            
            if case .failure(let response) = result {
                          
                        
            }
            
        }

    }
    
    @objc func GetAnAccount(){
        
        self.hiddenForChooseView()
        GetAnAccountView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)).show()
        
    }
    

    
    
    //MARK: - 展开列表

    @objc func showregionList(button:UIButton){
        self.view.endEditing(true)
        
        if button == regionBtn {
            self.regionTable.isHidden = false
            UIView.animate(withDuration: 0.3) {
                self.regionTable.alpha = 1
            }
        } else {
            roleTable.isHidden = false
            UIView.animate(withDuration: 0.3) {
                self.roleTable.alpha = 1
            }
        }
    }
    
    @objc func eyeBtnClicked(button:UIButton){
        button.isSelected = !button.isSelected
        if button.isSelected {
            self.pwdTf.isSecureTextEntry = false
        } else {
            self.pwdTf.isSecureTextEntry = true
        }
    }
    
    


    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
